using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
using System.IO;
using System.Reflection;
using System;
using System.Collections.Generic;
using Newtonsoft.Json;



public class UIFrameSpriteSheet
{
    static string uiPrefabPath = "prefab/ui";
    public static string uiFramePath = "tex/uiframe";

    class StringHashSetComparer: IEqualityComparer<HashSet<string>>
    {
        public bool Equals(HashSet<string> a, HashSet<string> b)
        {
            if (a.Count != b.Count)
            {
                return false;
            }

            foreach(var t in a)
            {
                if (!b.Contains(t))
                {
                    return false;
                }
            }

            return true;
        }

        public int GetHashCode(HashSet<string> a)
        {
            return 0;
        }
    }

    class PrefabInfo
    {
        public string path;
        public HashSet<string> texPaths = new HashSet<string>();
        public HashSet<string> childPrefabPaths = new HashSet<string>();
        public HashSet<string> parentPrefabPaths = new HashSet<string>();
    }

    [MenuItem("Custom/SpriteTool/CopyUIFrameChTex")]
    public static void CopyUIFrameChTex()
    {
        Dictionary<string, KeyValuePair<string, int>> hashNameMap = null;
        Dictionary<string, HashSet<string>> spTexMap = GetUIFrameSpriteSheetConfig(out hashNameMap);

        string str = CommonUtil.fileToString("Assets/uiframe_ch.txt");
        System.IO.StringReader reader = new StringReader(str);

        HashSet<string> s = new HashSet<string>();
        while (true)
        {
            string line = reader.ReadLine();
            if (line == null)
            {
                break;
            }

            s.Add(line);
        }

        Dictionary<string, HashSet<string>>.Enumerator e = spTexMap.GetEnumerator();
        while (e.MoveNext())
        {
            HashSet<string> tempSet = e.Current.Value;
            foreach (var sss in tempSet)
            {
                string ss = System.IO.Path.ChangeExtension(sss, "");
                ss = ss.Substring(0, ss.Length - 1);

                if (s.Contains(ss))
                {
                    LangResTool.CopyRes(sss, SystemLanguage.ChineseSimplified);
                    LangResTool.CopyRes(sss + ".meta", SystemLanguage.ChineseSimplified);
                }
            }
        }
    }

    [MenuItem("Custom/SpriteTool/DeleteChTex")]
    public static void DeleteChTex()
    {
        Dictionary<string, KeyValuePair<string, int>> hashNameMap = null;
        Dictionary<string, HashSet<string>> spTexMap = GetUIFrameSpriteSheetConfig(out hashNameMap);

        string str = CommonUtil.fileToString("Assets/uiframe_ch.txt");
        System.IO.StringReader reader = new StringReader(str);

        HashSet<string> s = new HashSet<string>();
        while (true)
        {
            string line = reader.ReadLine();
            if (line == null)
            {
                break;
            }

            s.Add(line);
        }

        string[] paths = System.IO.Directory.GetFiles("Assets/BundleRes/lang_res", "*", SearchOption.AllDirectories);
        for (int i = 0; i < paths.Length; i++)
        {
            string path = paths[i];
            if (path.EndsWith(".meta"))
            {
                continue;
            }

            string fileName = System.IO.Path.GetFileName(path);
            string realPath = "Assets/BundleRes/" + fileName.Replace("~", "/");

            if (realPath.Contains("Assets/BundleRes/tex/uiframe"))
            {
                bool found = false;
                Dictionary<string, HashSet<string>>.Enumerator e = spTexMap.GetEnumerator();
                while (e.MoveNext())
                {
                    HashSet<string> tempSet = e.Current.Value;
                    if (tempSet.Contains(realPath))
                    {
                        found = true;
                        break;
                    }

                    if (found)
                    {
                        break;
                    }
                }

                if (found)
                {
                    continue;
                }

                string ss = System.IO.Path.ChangeExtension(realPath, "");
                ss = ss.Substring(0, ss.Length - 1);
                if (!s.Contains(ss))
                {
                    //                        Debug.LogError("delete: " + realPath);
                    CommonUtil.Delete(path);
                }
            }
            else if (!System.IO.File.Exists(realPath))
            {
//                Debug.LogError("delete: " + realPath);
                CommonUtil.Delete(path);
            }
        }
    }

    [MenuItem("Custom/SpriteTool/RebuildSpriteSheetForUIFrame")]
    public static void RebuildSpriteSheetForUIFrame()
    {
        Dictionary<string, KeyValuePair<string, int>> hashNameMap = null;
        Dictionary<string, HashSet<string>> spTexMap = GetUIFrameSpriteSheetConfig(out hashNameMap);

        string spPath = CreateSpriteSheet.bundleDir[0];
        SpriteSheetConfig newConfig = EditorCommonTool.getRawSpriteSheetConfig(spPath);
        if(newConfig == null)
        {
            throw new Exception("raw sprite sheet config not found: " + spPath);
        }
        
        foreach (KeyValuePair<string, HashSet<string>> entry in spTexMap)
        {
            SpriteSheetInfo spInfo = null;
			if (!newConfig.TryGetValue(entry.Key, out spInfo))
			{
				spInfo = new SpriteSheetInfo();
				newConfig.Add(entry.Key, spInfo);
			}

			spInfo.is_from_interface = true;
			spInfo.multi_pack = true;
			spInfo.max_size = TexAttributeEx.MAX_SIZE;

            int len = 0;
            spInfo.dirs = new string[entry.Value.Count];
            foreach (string texPath in entry.Value)
            {
                string spTexPath = System.IO.Path.GetDirectoryName(texPath) + "/" + System.IO.Path.GetFileNameWithoutExtension(texPath);
                spTexPath = spTexPath.Replace(spPath + "/", "");
                spInfo.dirs[len] = spTexPath;
                len++;
            }
        }

        HashSet<string> delSet = new HashSet<string>();
        SpriteSheetConfig.Enumerator enumerator = newConfig.GetEnumerator();
        while (enumerator.MoveNext())
        {
            if (enumerator.Current.Value.is_from_interface)
            {
                if (!spTexMap.ContainsKey(enumerator.Current.Key))
                {
                    delSet.Add(enumerator.Current.Key);
                }
            }
        }

        foreach (string key in delSet)
        {
            newConfig.Remove(key);
        }

        SpriteSheetConfig oldConfig = EditorCommonTool.getRawSpriteSheetConfig(spPath);
        EditorCommonTool.setRawSpriteSheetConfig(newConfig, spPath);

        HashSet<string> changeSet = EditorCommonTool.getChangedSpriteSheetList(newConfig, oldConfig);
        SpriteSheetTool.modifySpritesheet (EditorCommonTool.need_rebuild_spritesheet_filepath, changeSet);

        CommonUtil.ObjectToJsonFile(hashNameMap, ResourcesEx.BundleResDir + "/" + "sprite_sheet_name_map.txt");
    }

    private static Dictionary<string, HashSet<string>> GetUIFrameSpriteSheetConfig(out Dictionary<string, KeyValuePair<string, int>> hashNameMap)
    {
        Dictionary<string, PrefabInfo> prefabInfos = new Dictionary<string, PrefabInfo>();

        
        string[] paths = System.IO.Directory.GetFiles(ResourcesEx.BundleResDir, "*.prefab", SearchOption.AllDirectories);

        List<string> assetPaths = new List<string>(paths);

        for(int i = 0; i < EditorBuildSettings.scenes.Length; i++)
        {
            EditorBuildSettingsScene scene = EditorBuildSettings.scenes[i];
            assetPaths.Add(scene.path);
        }
        
    
        for (int i = 0; i < assetPaths.Count; i++)
        {
            string path = assetPaths[i];
			path = path.Replace ("\\", "/");
            PrefabInfo prefabInfo = new PrefabInfo();
            prefabInfo.path = path;

            string[] dependencies = AssetDatabase.GetDependencies(path, false);
            for (int j = 0; j < dependencies.Length; j++)
            {
                string d = dependencies[j];

                string ext = System.IO.Path.GetExtension(d);
                for (int k = 0; k < MaterialTextureForETC1.exts.Length; k++)
                {
                    if (!d.Contains(uiFramePath))
                    {
                        continue;
                    }

                    if (ext == MaterialTextureForETC1.exts[k])
                    {
                        prefabInfo.texPaths.Add(d);
                        break;
                    }
                }

                if (d.Contains(".prefab") && d.Contains(uiPrefabPath) && d != path)
                {
                    prefabInfo.childPrefabPaths.Add(d);
                }
            }

            if (prefabInfo.childPrefabPaths.Count > 0 || prefabInfo.texPaths.Count > 0)
            {
                prefabInfos.Add(prefabInfo.path, prefabInfo);
            }
        }


        Dictionary<string, PrefabInfo>.Enumerator en = prefabInfos.GetEnumerator();
        while (en.MoveNext())
        {
            HashSet<string> removeChild = new HashSet<string>();
            foreach (string childPath in en.Current.Value.childPrefabPaths)
            {
                PrefabInfo childPrefab = null;
                if(prefabInfos.TryGetValue(childPath, out childPrefab))
                {
                    childPrefab.parentPrefabPaths.Add(en.Current.Value.path);
                }
                else
                {
                    removeChild.Add(childPath);
                }
            }

            foreach(string needRemoves in removeChild)
            {
                en.Current.Value.childPrefabPaths.Remove(needRemoves);
            }
        }

        HashSet<string> reomoveSet = new HashSet<string>();

        //merge leaf to parent node
//        while (reomoveSet.Count > 0)
//        {
//            foreach (string removePath in reomoveSet)
//            {
//                prefabInfos.Remove(removePath);
//            }
//            reomoveSet.Clear();
//
//            en = prefabInfos.GetEnumerator();
//            while (en.MoveNext())
//            {
//
//                if (en.Current.Value.childPrefabPaths.Count == 0 && en.Current.Value.parentPrefabPaths.Count == 1)
//                {
//                    foreach (string texPath in en.Current.Value.texPaths)
//                    {
//                        HashSet<string>.Enumerator parentEn = en.Current.Value.parentPrefabPaths.GetEnumerator();
//                        while (parentEn.MoveNext())
//                        {
//                            prefabInfos[parentEn.Current].texPaths.Add(texPath);
//                            prefabInfos[parentEn.Current].childPrefabPaths.Remove(en.Current.Key);
//                            reomoveSet.Add(en.Current.Key);
//                        }
//                    }
//                }
//            }
//        }



        Dictionary<string, HashSet<string>> texPrefabsMap = new Dictionary<string, HashSet<string>>();
        en = prefabInfos.GetEnumerator();
        while (en.MoveNext())
        {
            foreach(string texPath in en.Current.Value.texPaths)
            {
                HashSet<string> prefabs = null;
                if (!texPrefabsMap.TryGetValue(texPath, out prefabs))
                {
                    prefabs = new HashSet<string>();
                    texPrefabsMap.Add(texPath, prefabs);
                }

                prefabs.Add(en.Current.Key);
            }
        }

        hashNameMap = new Dictionary<string, KeyValuePair<string, int>>();

//        Dictionary<HashSet<string>, int> bbb = new Dictionary<HashSet<string>, int>(new StringHashSetComparer());
//        {
//            HashSet<string> s = new HashSet<string>();
//            s.Add("asdf");
//            bbb.Add(s, 0);
//        }
//
//        {
//            HashSet<string> s = new HashSet<string>();
//            s.Add("asdf");
//            bbb.Add(s, 0);
//        }

        Dictionary<HashSet<string>, HashSet<string>> shareTexPrefabMap = new Dictionary<HashSet<string>, HashSet<string>>(new StringHashSetComparer());

        Dictionary<string, HashSet<string>> spSet = new Dictionary<string, HashSet<string>>();
        foreach(KeyValuePair<string, HashSet<string>> entry in texPrefabsMap)
        {
            string spName = null;
            if (entry.Value.Count <= 1)
            {
                HashSet<string>.Enumerator prefabSetEn = entry.Value.GetEnumerator();
                prefabSetEn.MoveNext();
                spName = System.IO.Path.GetFileNameWithoutExtension(prefabSetEn.Current);
            }
            else
            {
                HashSet<string> texSet2 = null;
                if (!shareTexPrefabMap.TryGetValue(entry.Value, out texSet2))
                {
                    texSet2 = new HashSet<string>();
                    shareTexPrefabMap.Add(entry.Value, texSet2);
                }

                texSet2.Add(entry.Key);
                
                continue;
            }

            HashSet<string> texSet = null;
            if (!spSet.TryGetValue(spName, out texSet))
            {
                texSet = new HashSet<string>();
                spSet.Add(spName, texSet);
            }

            texSet.Add(entry.Key);
        }

        if (false)
        {
            int needMergeTexNum = 20;

            while (true)
            {
                KeyValuePair<HashSet<string>, HashSet<string>> temp = new KeyValuePair<HashSet<string>, HashSet<string>>(new HashSet<string>(), null);
                foreach (var entry in shareTexPrefabMap)
                {
                    if (temp.Value == null)
                    {
                        temp = entry;
                    }
                    else
                    {
                        if (entry.Value.Count < temp.Value.Count)
                        {
                            temp = entry;
                        }
                        else if (entry.Value.Count == temp.Value.Count)
                        {
                            if (entry.Key.Count < temp.Key.Count)
                            {
                                temp = entry;
                            }
                        }
                    }
                }

                if (temp.Value.Count >= needMergeTexNum)
                {
                    break;
                }

                if (shareTexPrefabMap.Count <= 1)
                {
                    break;
                }

                List<KeyValuePair<HashSet<string>, HashSet<string>>> tempList = new List<KeyValuePair<HashSet<string>, HashSet<string>>>();
                foreach (var entry in shareTexPrefabMap)
                {
                    if (entry.Key != temp.Key)
                    {
                        tempList.Add(entry);
                    }
                }

                Dictionary<HashSet<string>, float> similarityMap = new Dictionary<HashSet<string>, float>();
                for (int i = 0; i < tempList.Count; i++)
                {
                    float similarity = GetSimilarity(tempList[i].Key, temp.Key);
                    similarityMap.Add(tempList[i].Key, similarity);
                }

                tempList.Sort((a, b) =>
                    {
                        int r = a.Value.Count - b.Value.Count;
                        if (r != 0)
                        {
                            return r;
                        }

                        r = a.Key.Count - b.Key.Count;
                        if (r != 0)
                        {
                            return r;
                        }

                        float similarityA = similarityMap[a.Key];
                        float similarityB = similarityMap[b.Key];

                        float ft = Mathf.Sign(similarityB - similarityA);
                        int t = (int)ft;

                        if (t != ft)
                        {
                            throw new Exception("error not equal");
                        }

                        return t;
                    });

                HashSet<HashSet<string>> needMergeSet = new HashSet<HashSet<string>>(new StringHashSetComparer());
                needMergeSet.Add(tempList[0].Key);

//            string log = "";
//            for (int i = 0; i < tempList.Count; i++)
//            {
//                log += tempList[i].Value.Count + ": " + similarityMap[tempList[0].Key] + "\n";
//            }
//            Debug.LogError(log);

                float firstSimilarity = similarityMap[tempList[0].Key];

//            for (int i = 1; i < tempList.Count; i++)
//            {
//                float secondSimilarity = similarityMap[tempList[i].Key];
//                if (secondSimilarity != firstSimilarity || secondSimilarity == 0)
//                {
//                    break;
//                }
//            }

                HashSet<string> newKey = new HashSet<string>(temp.Key);
                foreach (var s in needMergeSet)
                {
                    newKey.Add(s);

                    temp.Value.Add(shareTexPrefabMap[s]);
                    shareTexPrefabMap.Remove(s);
                }

                HashSet<string> ts = new HashSet<string>();
                if (shareTexPrefabMap.TryGetValue(newKey, out ts))
                {
                    shareTexPrefabMap[newKey].Add(temp.Value);
                }
                else
                {
                    shareTexPrefabMap.Add(newKey, temp.Value);
                }

                shareTexPrefabMap.Remove(temp.Key);
            }
        }

        foreach (var entry in shareTexPrefabMap)
        {
            List<string> prefabPathList = new List<string>(entry.Key);
            prefabPathList.Sort();

            string commonSpriteSheetName = "c_";
            for (int i = 0; i < prefabPathList.Count; i++)
            {
                string prefabPath = prefabPathList[i];
                commonSpriteSheetName += "_" + prefabPath;
            }

            string spName = MD5Hash.GetMD5HashFromString(commonSpriteSheetName);

            hashNameMap.Add(spName, new KeyValuePair<string, int>(commonSpriteSheetName, entry.Value.Count));

            HashSet<string> texSet = new HashSet<string>();
            foreach (var texPath in entry.Value)
            {
                texSet.Add(texPath);
            }
            spSet.Add(spName, texSet);
        }

        return spSet;
    }

    static float GetSimilarity(HashSet<string> a, HashSet<string> b)
    {
        int sa = 0;
        foreach (string ta in a)
        {
            if (b.Contains(ta))
            {
                sa++;
            }
        }

        return (float)(sa * sa) / (a.Count * b.Count);
    }

//    [MenuItem("CONTEXT/RectTransform/MakeWindow")]
//    private static void NewMenuOption1(MenuCommand menuCommand)
//    {
//        RectTransform transform = menuCommand.context as RectTransform;
//        
//        transform.anchorMax = new Vector2(1, 1);
//        transform.anchorMin = new Vector2(0, 0);
//        transform.pivot = new Vector2(0.5f,0.5f);
//        transform.sizeDelta = new Vector2(0,0);
//        transform.localPosition = new Vector3(0, 0, 0);
//        transform.localScale = new Vector3(1, 1, 1);
//        transform.rotation = Quaternion.identity;
//        GraphicRaycaster gr = transform.gameObject.GetComponent<GraphicRaycaster>();
//        if (gr != null)
//        {
//            GameObject.DestroyImmediate(gr,true);
//        }
//        CanvasScaler cs = transform.gameObject.GetComponent<CanvasScaler>();
//        if (cs != null)
//        {
//			GameObject.DestroyImmediate(cs, true);
//        }
//        Canvas c = transform.gameObject.GetComponent<Canvas>();
//        if (c != null)
//        {
//			GameObject.DestroyImmediate(c, true);
//        }
//    }
//
//    [MenuItem("Custom/Prefab/ReMakeAllWindow")]
//    private static void NewMenuOption()
//    {
//        string[] paths = Directory.GetFiles("Assets/", "*.prefab", SearchOption.AllDirectories);
//        for (int i = 0; i < paths.Length; i++)
//        {
//            UnityEngine.Object realPrefab = AssetDatabase.LoadAssetAtPath(paths[i], typeof(UnityEngine.Object));
//            GameObject prefab = PrefabUtility.InstantiatePrefab(realPrefab) as GameObject;
//
//            Animator[] animators = prefab.GetComponentsInChildren<Animator>();
//            if (animators == null || animators.Length ==0)
//            {
//				GameObject.DestroyImmediate(prefab);
//                continue;
//            }
//           
//            Debug.Log(paths[i]);
//            for (int j = 0; j < animators.Length;j++ )
//            {
//                Animator animator =animators[j];
//                Animation am = animator.gameObject.GetComponent<Animation>();
//                if (am!=null)
//                {
//					GameObject.DestroyImmediate(am);
//                }
//				#if UNITY_5
//				animator.cullingMode = AnimatorCullingMode.CullUpdateTransforms;
//				#else
//				animator.cullingMode = AnimatorCullingMode.BasedOnRenderers;
//				#endif
//            }
//
//            PrefabUtility.ReplacePrefab(prefab, realPrefab, ReplacePrefabOptions.Default);
//            GameObject.DestroyImmediate(prefab);
//        }
//    }
//	
//    private static WindowPrefabInfo GetWindowInfo(List<WindowPrefabInfo> result,string name)
//    {
//        for (int i = 0; i < result.Count; i++)
//        {
//            WindowPrefabInfo info = result[i];
//			if(info == null)
//			{
//				Debug.LogError("info is null");
//				continue;
//			}
//            if (info.windowName.Equals(name))
//            {
//                return info;
//            }
//        }
//        return null;
//    }
//
//    private static void GetRefWindowGroup(List<WindowPrefabInfo> allWindowInfo,List<string> blackList, HashSet<string> refWindowSet, WindowPrefabInfo info, string name = null)
//    {
//		if(info == null)
//		{
//			Debug.LogError("info is null:" + name);
//			return;
//		}
//        refWindowSet.Add(info.windowName);
//		foreach (string s in info.referenceWindowName)
//        {
//			if (blackList.Contains(s))
//            {
//                continue;
//            }
//			if (refWindowSet.Add(s))
//            {
//				WindowPrefabInfo refInfo = GetWindowInfo(allWindowInfo, s);
//                if (refInfo!=null)
//                {
//                    GetRefWindowGroup(allWindowInfo, blackList,refWindowSet, refInfo, null);
//                }
//            }
//
//        }
//    }
//
//
//
//
//    
//
//
//	public static Dictionary<string, HashSet<string>> getPrefabGroup(ref List<string> shareWindow, ref List<WindowPrefabInfo> allWindowInfo)
//	{
//		//add prefab logic, mainui
//		//创建分组的黑名单
//		List<string> nonReferenceBlackList = new List<string>();
//		nonReferenceBlackList.Add(typeof(UISpeedupWindow).Name);// 这个是个基类
//
//		shareWindow = new List<string>();
//		shareWindow.Add(typeof(UIPopupTipsFullWindow).Name);
//		shareWindow.Add(typeof(UIItemPopWindow).Name);
//		shareWindow.Add(typeof(UIUseItemWindow).Name);
////		shareWindow.Add(typeof(UIUseItemViewController).Name);
//		shareWindow.Add(typeof(UIPropsTipsWindow).Name);
////		shareWindow.Add(typeof(UIWriteMailViewController).Name);
//		shareWindow.Add(typeof(UIPopupOneButtonDialog).Name);
//		shareWindow.Add(typeof(UIPopupTipsWindow).Name);
//		shareWindow.Add(typeof(UITwoButtonDialog).Name);
////		shareWindow.Add(typeof(UIPopupGoldCostDialog).Name);
//		shareWindow.Add(typeof(UIDetailWindow).Name);
//		shareWindow.Add(typeof(UIDebugInfoWindow).Name);
//		shareWindow.Add(typeof(UINPCDialog).Name);
//		
//
//		//后面的这些，不是特别公共
//		//		shareWindow.Add(typeof(UISubCityGeneralSetController).Name);
//		//		shareWindow.Add(typeof(UIGeneralCardViewController).Name);
//		//		shareWindow.Add(typeof(UIGuildSetLvView).Name);
//		//		shareWindow.Add(typeof(UIEvonyStoreBuyWindow).Name);
//		
//		Dictionary<string, HashSet<string>> forceRootWindow = new Dictionary<string, HashSet<string>>();
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UINotificationWindow).Name);
//			hashSet.Add(typeof(UIGuideWindow).Name);
//			hashSet.Add(typeof(UIUnionGiftView).Name);
//			forceRootWindow.Add("ui_guide", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UIEvonyViewController2).Name);
//			hashSet.Add(typeof(UIReconectWindow).Name);
//			hashSet.Add(typeof(UIResourcesViewController).Name);
//			hashSet.Add(typeof(UIInfoPopWindow).Name);
//			hashSet.Add(typeof(UIServerErrorDialog).Name);
//			forceRootWindow.Add("ui_common", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UIDressUpViewController).Name);
//			hashSet.Add(typeof(UISnowballPurchaseWindow).Name);
//			forceRootWindow.Add("ui_dress", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UILordProfileViewController).Name);
//			forceRootWindow.Add("ui_lord_profile", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UILordViewController).Name);
//			hashSet.Add(typeof(UIOtherLordViewController).Name);
//			forceRootWindow.Add("ui_lord", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UIGuildViewController).Name);
//			hashSet.Add(typeof(UIGuildGarrisonViewController).Name);
//			hashSet.Add(typeof(UIGuildInfoViewController).Name);
//			hashSet.Add(typeof(UIGuildUnionWarRecordView).Name);
//			forceRootWindow.Add("ui_guild", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UIBuildingPopupTechnology).Name);
//			forceRootWindow.Add("ui_technology", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UIBuildingCreate).Name);
//			forceRootWindow.Add("ui_building_create", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UIWallRepair).Name);
//			forceRootWindow.Add("ui_building", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UIMailViewController).Name);
//			forceRootWindow.Add("ui_mail", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UIEvonyStoreViewController).Name);
//			hashSet.Add(typeof(UIStoreViewController).Name);
//			hashSet.Add(typeof(UIMonthMineViewController).Name);
//			forceRootWindow.Add("ui_store", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UISynthesizeViewController).Name);
//			forceRootWindow.Add("ui_synthesize", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UIItemViewController).Name);
//			hashSet.Add(typeof(UIArrestWarrantWindow).Name);
//			hashSet.Add(typeof(UIOpenTheBoxlist).Name);
//			hashSet.Add(typeof(UICollectItemWindow).Name);
//			forceRootWindow.Add("ui_item", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UIGeneralProfileViewController).Name);
//			hashSet.Add(typeof(UIAltarReliveGeneral2).Name);//rename
//			hashSet.Add(typeof(UITavernHireGeneral).Name);//rename
//			hashSet.Add(typeof(UIGeneralSkillBookView).Name);
//			hashSet.Add(typeof(UIMyGeneralsViewController).Name);
//			hashSet.Add(typeof(UIGeneralCardWindowController).Name);
//			forceRootWindow.Add("ui_general", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UIForgeViewController).Name);
//			hashSet.Add(typeof(UIDestructWindow).Name);
//			forceRootWindow.Add("ui_forge", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UIExpeditionViewController).Name);
//			forceRootWindow.Add("ui_expedition", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UIBuildingPrisonView).Name);
//			hashSet.Add(typeof(UIBuildingPrisonWorkView).Name);
//			forceRootWindow.Add("ui_prison", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UIBuildingArmsField).Name);
//			forceRootWindow.Add("ui_arms_field", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UIBuildingPopupTrainTroop).Name);
//			forceRootWindow.Add("ui_train_army", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UIRotaryTableController2).Name);
//			forceRootWindow.Add("ui_rotary_table", hashSet);
//		}
//
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UISpeakerWindow).Name);
//			forceRootWindow.Add("ui_speaker", hashSet);
//		}
//
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UINewSubCityController).Name);
//			hashSet.Add(typeof(UISubCityController).Name);
//			hashSet.Add(typeof(UISubCityListController).Name);
//			hashSet.Add(typeof(UISubCitySummaryViewController).Name);//rename
//			hashSet.Add(typeof(UIOccupiedSubCity).Name);
//			hashSet.Add(typeof(UIWorldSubCityPopWindow).Name);
//			hashSet.Add(typeof(UISubCityImproveQualitView).Name);
//			forceRootWindow.Add("ui_sub_city", hashSet);
//		}
//
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UIRuinViewController).Name);//rename
//			hashSet.Add(typeof(UIRuinNoteWindow).Name);
//			hashSet.Add(typeof(UIRuinDetailWindow).Name);
//			forceRootWindow.Add("ui_ruin", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UICoordinatePopWindow).Name);
//			hashSet.Add(typeof(UITroopInfoPopWindow).Name);//rename
//			hashSet.Add(typeof(UIWorldCollectionWindow).Name);//rename
//			hashSet.Add(typeof(UIWorldMonsterPopWindow).Name);//rename
//			hashSet.Add(typeof(UISantaClausView).Name);//rename
//			hashSet.Add(typeof(UIWorldResourcePointDetail).Name);
//			forceRootWindow.Add("ui_world_window", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UIKingdomManageViewController).Name);
//			hashSet.Add(typeof(UIKingdomChangeKing2ViewController).Name);
//			hashSet.Add(typeof(UIKingdomInfoView).Name);
//			hashSet.Add(typeof(UIKingdomNPCAttack).Name);
//			hashSet.Add(typeof(UIPatrolViewController).Name);
//			hashSet.Add(typeof(UIKingdomGiftController).Name);
//			forceRootWindow.Add("ui_kingdom", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UISettingsViewController).Name);//rename
//			forceRootWindow.Add("ui_setting", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UIBlackMarketViewController).Name);
//			hashSet.Add(typeof(UITaxGoldViewController).Name);
//			hashSet.Add(typeof(UITaxesViewController).Name);
//			hashSet.Add(typeof(UITransportViewController).Name);
//			forceRootWindow.Add("ui_black_market", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UICityBufferViewController).Name);
//			forceRootWindow.Add("ui_buffer", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UISignInView).Name);
//			hashSet.Add(typeof(UIEventsPackageWindow).Name);
//			hashSet.Add(typeof(UIOnlineReward).Name);
//			forceRootWindow.Add("ui_daily_reward", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UIUpgradeWindow).Name);
//			hashSet.Add(typeof(UILordUpgrade).Name);
//			forceRootWindow.Add("ui_upgrade", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UIAgeUpgraded).Name);
//			forceRootWindow.Add("ui_age_upgrade", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UIMilitaryInfoViewController).Name);
//			forceRootWindow.Add("ui_watch_tower", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UISacrificeViewController).Name);
//			forceRootWindow.Add("ui_sacrifice", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UITaskViewController).Name);
//			hashSet.Add(typeof(UIActivityTaskViewController).Name);
//			forceRootWindow.Add("ui_task", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UIVipViewController).Name);//rename
//			forceRootWindow.Add("ui_vip", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UIWorldMapFavoriteViewController).Name);
//			forceRootWindow.Add("ui_favorite", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UIFriendsViewController).Name);
//			hashSet.Add(typeof(UIChatShareWindow).Name);
//			forceRootWindow.Add("ui_friend", hashSet);
//		}
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UICutsceneDialog).Name);
//			hashSet.Add(typeof(UICutsceneGeneralDialog).Name);
//			forceRootWindow.Add("ui_cutscene", hashSet);
//		}
//
//		{
//			HashSet<string> hashSet = new HashSet<string>();
//			hashSet.Add(typeof(UIServerActive1Controller).Name);
//			hashSet.Add(typeof(UIServerActive2Controller).Name);
//			hashSet.Add(typeof(UILimitedGoldViewController).Name);
//			hashSet.Add(typeof(UIChargeLimitedWindow).Name);
//			hashSet.Add(typeof(UIReceiveItemWindow).Name);
//			forceRootWindow.Add("ui_active", hashSet);
//		}
//		
////		HashSet<string> mergePointWindow = new HashSet<string>();
////
//		allWindowInfo = new List<WindowPrefabInfo>();
//		Type[] allType = Assembly.GetAssembly(typeof(Window)).GetTypes();
//		for (int i = 0; i < allType.Length;i++ )
//		{
//			Type t= allType[i];
//			if (!t.IsSubclassOf(typeof(Window)) || t.Equals(typeof(Window)) || t.Equals(typeof(PopupWindow)) || t.Equals(typeof(Dialog)) || t.Equals(typeof(NativeWindow)) || t.IsSubclassOf(typeof(NativeWindow)))
//			{
//				continue;
//			}
//
//			WindowPrefabInfo info = CheckWindowReferenceTool.GatherPrefabRef(t);
//			if(info == null)
//			{
//				continue;
//			}
//			allWindowInfo.Add(info);
//		}
//		Debug.Log(allWindowInfo.Count);
//		
//		
//		nonReferenceBlackList.AddRange(shareWindow);
//
//		{
//			Dictionary<string, HashSet<string>>.Enumerator enumerator = forceRootWindow.GetEnumerator();
//			while(enumerator.MoveNext())
//			{
//				foreach(string script in enumerator.Current.Value)
//				{
//					nonReferenceBlackList.Add(script);
//				}
//			}
//		}
//		
//		//给窗口分组
//		Dictionary<string, HashSet<string>> windowGroup = new Dictionary<string, HashSet<string>>();
//		Dictionary<string, HashSet<string>>.Enumerator enumerator2 = forceRootWindow.GetEnumerator();
//		while(enumerator2.MoveNext())
//		{
//			foreach(string script in enumerator2.Current.Value)
//			{
//				WindowPrefabInfo info = GetWindowInfo(allWindowInfo, script);
//				HashSet<string> refWindowSet = new HashSet<string>();
//				GetRefWindowGroup(allWindowInfo, nonReferenceBlackList, refWindowSet, info, script);
//				bool needAdd = true;
//				//			for (int j = 0; j < windowGroup.Count; j++)
//				//			{
//				//				HashSet<string> temp = windowGroup[j];
//				//				HashSet<string> largeSet = temp.Count >= refWindowSet.Count ? temp : refWindowSet;
//				//				HashSet<string> smallSet = temp.Count >= refWindowSet.Count ? refWindowSet : temp;
//				//				if (smallSet.IsSubsetOf(largeSet))
//				//				{
//				//					needAdd = false;
//				//					windowGroup[j] = largeSet;
//				//				}
//				//			}
//				if (needAdd)
//				{
//					windowGroup.Add(script, refWindowSet);
//				}
//			}
//		}
//		
//		Debug.Log(windowGroup.Count);
//		
//		for (int i = 0; i < allWindowInfo.Count; i++)
//		{
//			WindowPrefabInfo info = allWindowInfo[i];
//			if (nonReferenceBlackList.Contains(info.windowName))
//			{
//				continue;
//			}
//
//			HashSet<string> refWindowSet = new HashSet<string>();
//			GetRefWindowGroup(allWindowInfo,nonReferenceBlackList, refWindowSet, info);
//			bool needAdd = true;
//			Dictionary<string, HashSet<string>> replaceSet = new Dictionary<string, HashSet<string>>();
//			Dictionary<string, HashSet<string>>.Enumerator enumerator3 = windowGroup.GetEnumerator();
//			while(enumerator3.MoveNext())
//			{
//				HashSet<string> temp = enumerator3.Current.Value;
//				HashSet<string> largeSet = temp.Count >= refWindowSet.Count ? temp : refWindowSet;
//				HashSet<string> smallSet = temp.Count >= refWindowSet.Count ? refWindowSet : temp;
//				if(smallSet.IsSubsetOf(largeSet))
//				{
//					needAdd = false;
//					replaceSet[enumerator3.Current.Key] = largeSet;
//				}
//			}
//
//			if (needAdd)
//			{
//				windowGroup.Add("nogroup_" + i, refWindowSet);
//			}
//			else
//			{
//				Dictionary<string, HashSet<string>>.Enumerator enumerator4 = replaceSet.GetEnumerator();
//				while(enumerator4.MoveNext())
//				{
//					windowGroup[enumerator4.Current.Key] = enumerator4.Current.Value;
//				}
//			}
//		}
//		
//		//		HashSet<HashSet<string>> groupSet = new HashSet<HashSet<string>>(windowGroup);
//		//		windowGroup.Clear();
//		//		windowGroup.AddRange(groupSet);
//		//		//统计窗口使用次数
//		//		HashSet<string> windowNameSet = new HashSet<string>();
//		//		Dictionary<string, int> windowUseCount = new Dictionary<string, int>();
//		//		for (int i = 0; i < windowGroup.Count; i++)
//		//		{
//		//			HashSet<string> temp = windowGroup[i];
//		//			foreach (string name in temp)
//		//			{
//		//				windowNameSet.Add(name);
//		//				if (windowUseCount.ContainsKey(name))
//		//				{
//		//					int count = windowUseCount[name];
//		//					count++;
//		//					windowUseCount[name] = count;
//		//					mergePointWindow.Add(name);
//		//				}
//		//				else
//		//				{
//		//					windowUseCount.Add(name, 1);
//		//				}
//		//			}
//		//		}
//		//		//当有两个window公用window时，merge这两个window的树
//		//		List<HashSet<string>> mergeResult = new List<HashSet<string>>();
//		//		foreach (string mergePoint in mergePointWindow)
//		//		{
//		//			List<string> merge = new List<string>();
//		//			for (int i = 0; i < windowGroup.Count; i++)
//		//			{
//		//				HashSet<string> temp = windowGroup[i];
//		//				if (temp.Contains(mergePoint))
//		//				{
//		//					windowGroup.RemoveAt(i);
//		//					i--;
//		//					merge.AddRange(temp);
//		//				}
//		//			}
//		//			if(merge.Count >0){
//		//				mergeResult.Add(new HashSet<string>(merge));
//		//			}
//		//		}
//		//		windowGroup.AddRange(mergeResult);
//		
//		//log
//		//		//输出 窗口引用次数log
//		//		List<string> windowNameList = new List<string>(windowNameSet);
//		//		windowNameList.Sort((string a, string b) => {
//		//			int ac = windowUseCount[a];
//		//			int bc = windowUseCount[b];
//		//			return  bc-ac;
//		//		});
//		//		string uselog = "";
//		//		for (int i = 0; i < windowNameList.Count;i++ )
//		//		{
//		//			uselog += windowNameList[i] + ":" + windowUseCount[windowNameList[i]]+"\r\n";
//		//		}
//		//		Debug.Log(uselog);
//		//		
//		//log
//		//		//输出 窗口分组log
//		//		string log="";
//		//		for (int i = 0; i < windowGroup.Count;i++ )
//		//		{
//		//			log += "group "+i+" ";
//		//			HashSet<string> temp = windowGroup[i];
//		//			foreach(string name in temp)
//		//			{
//		//				log += name + "-";
//		//			}
//		//			log += "\r\n";
//		//		}
//		//		Debug.Log(log);
//
//
//		//按照window分组，统计prefab分组
//		Dictionary<string, HashSet<string>> prefabGroup = new Dictionary<string, HashSet<string>>();
//		
//		Dictionary<string, HashSet<string>>.Enumerator winGroupEnumerator = windowGroup.GetEnumerator();
//		while(winGroupEnumerator.MoveNext())
//		{
//			HashSet<string> prefabPath = new HashSet<string>();
//			HashSet<string> temp = winGroupEnumerator.Current.Value;
//			foreach (string name in temp)
//			{
//				WindowPrefabInfo info = GetWindowInfo(allWindowInfo, name);
//				if(info == null)
//				{
//					Debug.LogError("info is null: " + name);
//				}
//
//				if(info.windwoPrefabPath == null)
//				{
//					Debug.LogError("prefabPath is null:" + info.windowName);
//					Debug.LogError("window not added:" + info.windowName);
//					continue;
//				}
//
//				prefabPath.Add(info.windwoPrefabPath);
//				foreach(string path in info.referencePrefabPath)
//				{
//					if(prefabPath == null)
//					{
//						Debug.LogError("prefabPath is null:" + info.windwoPrefabPath);
//					}
//					prefabPath.Add(path);
//				}
//			}
//			prefabGroup.Add(winGroupEnumerator.Current.Key, prefabPath);
//		}
//
//
//		Dictionary<string, HashSet<string>>.Enumerator en1 = forceRootWindow.GetEnumerator();
//		while(en1.MoveNext())
//		{
//			HashSet<string> newSet = new HashSet<string>();
//			foreach(string set in en1.Current.Value)
//			{
//				HashSet<string> oldSet = prefabGroup[set];
//				foreach(string oldPath in oldSet)
//				{
//					newSet.Add(oldPath);
//				}
//				prefabGroup.Remove(set);
//			}
//			
//			prefabGroup[en1.Current.Key] = newSet;
//		}
//		
//		//		HashSet<string> prefabNameSet = new HashSet<string>();
//		//		Dictionary<string, int> prefabUseCount = new Dictionary<string, int>();
//		//		for (int i = 0; i < prefabGroup.Count; i++)
//		//		{
//		//			HashSet<string> temp = prefabGroup[i];
//		//			foreach (string path in temp)
//		//			{
//		//				prefabNameSet.Add(path);
//		//				if (prefabUseCount.ContainsKey(path))
//		//				{
//		//					int count = prefabUseCount[path];
//		//					count++;
//		//					prefabUseCount[path] = count;
//		//				}
//		//				else
//		//				{
//		//					prefabUseCount.Add(path, 1);
//		//				}
//		//			}
//		//		}
//		
//		HashSet<string> commonWindowPrefabs = null;
//		if(!prefabGroup.TryGetValue("ui_common", out commonWindowPrefabs))
//		{
//			commonWindowPrefabs = new HashSet<string>();
//			prefabGroup.Add("ui_common", commonWindowPrefabs);
//		}
//
//		
//		foreach (string name in shareWindow)
//		{
//			WindowPrefabInfo info = GetWindowInfo(allWindowInfo, name);
//			commonWindowPrefabs.Add(info.windwoPrefabPath);
//			foreach(string path in info.referencePrefabPath)
//			{
//				commonWindowPrefabs.Add(path);
//			}
//		}
//
//		GameObject obj = AssetDatabase.LoadAssetAtPath("Assets/Editor/PrefabGroup.prefab", typeof(GameObject)) as GameObject;
//		PrefabGroup prefabGroup2 = obj.GetComponent<PrefabGroup>();
//		for(int i = 0; i < prefabGroup2.groups.Length; i++)
//		{
//			Group group = prefabGroup2.groups[i];
//			if(group.prefabs.Length > 0)
//			{
//				HashSet<string> prefabObjs = null;
//				if(!prefabGroup.TryGetValue(group.name, out prefabObjs))
//				{
//					prefabObjs = new HashSet<string>();
//					prefabGroup.Add(group.name, prefabObjs);
//				}
//
//				for(int j = 0; j < group.prefabs.Length; j++)
//				{
//					if(group.prefabs[j] == null)
//					{
//						Debug.LogError("missing prefab:" + group.name + " " + j);
//					}
//					string prefabPath = AssetDatabase.GetAssetPath(group.prefabs[j]);
//					prefabPath = prefabPath.Replace(CreateSpriteSheet.bundleDir + "/", "");
//					prefabPath = prefabPath.Replace(".prefab", "");
//					if(string.IsNullOrEmpty(prefabPath))
//					{
//						Debug.LogError("path is null:" + prefabPath);
//					}
//					prefabObjs.Add(prefabPath);
//				}
//			}
//		}
//
//		return prefabGroup;
//	}
//
//	public const string dir = "UIInterface";
//
//	static public int SortByLen (string a, string b) { return a.Length - b.Length; }
//
//	[MenuItem("Custom/Sprite/GatherPrefabUI")]
//	private static void GatherPrefabUI()
//	{
//		{
//			HashSet<string> ret = MaterialTextureForETC1.CheckSameNameTex();
//			if(ret.Count > 0)
//			{
//				string str = "";
//				foreach(string p in ret)
//				{
//					str += p + "\n";
//				}
//				
//				Debug.LogError("repeat name:\n" + str);
//				return;
//			}
//		}
//
//		{
//			HashSet<string> ret = MaterialTextureForETC1.CheckSpaceNameTex();
//			if(ret.Count > 0)
//			{
//				string str = "";
//				foreach(string p in ret)
//				{
//					str += p + "\n";
//				}
//				
//				Debug.LogError("space name:\n" + str);
//				return;
//			}
//		}
//		
//		{
//			Dictionary<string, HashSet<string>> ret = MaterialTextureForETC1.CheckPrefabErrorResReference();
//			if(ret.Count > 0)
//			{
//				string str = "";
//				Dictionary<string, HashSet<string>>.Enumerator enumerator = ret.GetEnumerator();
//				while(enumerator.MoveNext())
//				{
//					str += enumerator.Current.Key + ":\n";
//					foreach(string p in enumerator.Current.Value)
//					{
//						str += "\t" + p + "\n";
//					}
//				}
//				
//				
//				Debug.LogError("use error res:\n" + str);
//				return;
//			}
//		}
//
//		List<string> shareWindow = null;
//		List<WindowPrefabInfo> allWindowInfo = null;
//		Dictionary<string, HashSet<string>> prefabGroup = getPrefabGroup(ref shareWindow, ref allWindowInfo);
//
//		Dictionary<string, HashSet<string>>.Enumerator tempE = prefabGroup.GetEnumerator();
//		while(tempE.MoveNext())
//		{
//			if(tempE.Current.Key.StartsWith("nogroup"))
//			{
//				if(tempE.Current.Value.Count > 0)
//				{
//					HashSet<string>.Enumerator t = tempE.Current.Value.GetEnumerator();
//					t.MoveNext();
//
//					Debug.LogError("nogroup:" + tempE.Current.Key + " " + t.Current);
//				}
//			}
//		}
//
//		string prefabGroupStr = JsonConvert.SerializeObject(prefabGroup, Formatting.Indented);
//		CommonUtil.stringToFile("prefab_group.txt", prefabGroupStr);
//
//
//		HashSet<string> notFoundPrefab = new HashSet<string>();
//		string[] prefabs = Directory.GetFiles(CreateSpriteSheet.bundleDir, "*.prefab", SearchOption.AllDirectories);
//		for(int i = 0; i < prefabs.Length; i++)
//		{
//			string prefabPath = prefabs[i];
////			if(prefabPath.Contains("/UITechnologyDialog"))
////			{
////				Debug.Log("");
////			}
//
//			if(prefabPath.Contains("UIPackage"))
//			{
//				continue;
//			}
//			if(prefabPath.Contains("RawUI/Prefabs/NPC"))
//			{
//				continue;
//			}
//			if(prefabPath.Contains("Prefabs/BuildingModel/Evony"))
//			{
//				continue;
//			}
//			if(prefabPath.Contains("RawUI/Prefabs/Army"))
//			{
//				continue;
//			}
//
//
//			prefabPath = prefabPath.Replace(CreateSpriteSheet.bundleDir + "/", "");
//			prefabPath = prefabPath.Replace(".prefab", "");
//
//			bool found = false;
//			Dictionary<string, HashSet<string>>.Enumerator enumerator = prefabGroup.GetEnumerator();
//			while(enumerator.MoveNext() && !found)
//			{
////				if(enumerator.Current.Key.Contains("ui_technology"))
////				{
////					Debug.Log("");
////
////					foreach(string s in enumerator.Current.Value)
////					{
////						int len = s.Length;
////						int lne2 = prefabPath.Length;
////						
////						for(int k = 0; k < len; k++)
////						{
////							char c = s[k];
////							char c2 = prefabPath[k];
////							if(c != c2)
////							{
////								break;
////							}
////						}
////
////						if(s == prefabPath)
////						{
////							found = true;
////							break;
////						}
////					}
////				}
//
//				if(enumerator.Current.Value.Contains(prefabPath))
//				{
//					found = true;
//					break;
//				}
//
//
//			}
//
//			if(!found)
//			{
//				notFoundPrefab.Add(prefabPath);
//			}
//		}
//
////		List<string> prefabList = new List<string>(notFoundPrefab);
////		prefabList.Sort(SortByLen);
//		string notFoundPrefabStr = JsonConvert.SerializeObject(notFoundPrefab, Formatting.Indented);
//		CommonUtil.stringToFile("not_found_prefab.txt", notFoundPrefabStr);
//
//		HashSet<string> notFoundWindowSet = new HashSet<string>();
//		foreach(string prefabPath in notFoundPrefab)
//		{
//			string realPrefabPath = CreateSpriteSheet.bundleDir + "/" + prefabPath + ".prefab";
//			GameObject obj = AssetDatabase.LoadAssetAtPath(realPrefabPath, typeof(GameObject)) as GameObject;
//			if(obj == null)
//			{
//				Debug.LogError("obj is null: " + realPrefabPath);
//			}
//			Window[] window = obj.GetComponents<Window>();
//			if(window.Length <= 0)
//			{
//				window = obj.GetComponentsInChildren<Window>();
//			}
//
//			if(window.Length > 0)
//			{
//				notFoundWindowSet.Add(realPrefabPath);
//			}
//		}
//		string notFoundWindowStr = JsonConvert.SerializeObject(notFoundWindowSet, Formatting.Indented);
//		CommonUtil.stringToFile("not_found_window.txt", notFoundWindowStr);
//
//
////		string str2 = JsonConvert.SerializeObject(prefabGroup, Formatting.Indented);
////		File.WriteAllText("test3.txt", str2);
////		return;
//
//		Dictionary<string, string> idMap = MaterialTextureForETC1.getSpriteIdInfoMap2(CreateSpriteSheet.bundleDir);
//		//find common group sprite path
//		Dictionary<string, HashSet<string>> spriteGroup = new Dictionary<string, HashSet<string>>();
//		Dictionary<string, HashSet<string>>.Enumerator e = prefabGroup.GetEnumerator();
//		while(e.MoveNext())
//		{
//			if(e.Current.Key != "ui_common")
//			{
//				continue;
//			}
//			
//			HashSet<string> prefabPaths = e.Current.Value;
//			HashSet<string> spritePaths = new HashSet<string>();
//			foreach(string path in prefabPaths)
//			{
//				string realPath = CreateSpriteSheet.bundleDir + "/" + path + ".prefab";
//				if(!File.Exists(realPath))
//				{
//					realPath = CreateSpriteSheet.localBundleDir + "/" + path + ".prefab";
//				}
//				
//				HashSet<string> temps = MaterialTextureForETC1.getPrefabSpritePath(realPath, idMap);
//				if(temps == null)
//				{
//					Debug.Log("null pointer: " + realPath);
//				}
//
//				foreach(string temp in temps)
//				{
//					if(temp.Contains(dir))
//					{
//						spritePaths.Add(temp);
//					}
//				}
//			}
//			spriteGroup.Add(e.Current.Key, spritePaths);
//		}
//
//
//		//find other group sprite path
//		HashSet<string> commonGroup = spriteGroup["ui_common"];
//		e = prefabGroup.GetEnumerator();
//		while(e.MoveNext())
//		{
//			if(e.Current.Key == "ui_common")
//			{
//				continue;
//			}
//			HashSet<string> prefabPaths = e.Current.Value;
//			HashSet<string> spritePaths = new HashSet<string>();
//			foreach(string path in prefabPaths)
//			{
//				string realPath = CreateSpriteSheet.bundleDir + "/" + path + ".prefab";
//				if(!File.Exists(realPath))
//				{
//					realPath = CreateSpriteSheet.localBundleDir + "/" + path + ".prefab";
//				}
//
//				HashSet<string> temps = MaterialTextureForETC1.getPrefabSpritePath(realPath, idMap);
//				if(temps == null)
//				{
//					Debug.Log("null pointer:" + realPath + " " + e.Current.Key);
//				}
//				foreach(string temp in temps)
//				{
//					if(temp.Contains(dir) && !commonGroup.Contains(temp))
//					{
//						spritePaths.Add(temp);
//					}
//				}
//			}
//			spriteGroup.Add(e.Current.Key, spritePaths);
//		}
//
//		HashSet<string> uiMainGroup = spriteGroup["ui_main"];
//
////		//merge to common
//		Dictionary<string, int> spriteCountMap = new Dictionary<string, int>();
//		e = spriteGroup.GetEnumerator();
//		while(e.MoveNext())
//		{
//			if(e.Current.Key == "ui_common")
//			{
//				continue;
//			}
//
//			foreach(string spritePath in e.Current.Value)
//			{
//				if(spriteCountMap.ContainsKey(spritePath))
//				{
//					spriteCountMap[spritePath]++;
//				}
//				else
//				{
//					spriteCountMap.Add(spritePath, 1);
//				}
//			}
//		}
//
////		HashSet<string> delGroup = new HashSet<string>();
////		Dictionary<string, int>.Enumerator ec = spriteCountMap.GetEnumerator();
////		while(ec.MoveNext())
////		{
////			if(ec.Current.Value > 1)
////			{
////				e = spriteGroup.GetEnumerator();
////				while(e.MoveNext())
////				{
////					if(e.Current.Key == "ui_common")
////					{
////						e.Current.Value.Add(ec.Current.Key);
////					}
////					else
////					{
////						e.Current.Value.Remove(ec.Current.Key);
////						if(e.Current.Value.Count <= 0)
////						{
////							delGroup.Add(e.Current.Key);
////						}
////					}
////				}
////			}
////		}
////
////		foreach(string key in delGroup)
////		{
////			spriteGroup.Remove(key);
////		}
//
//		HashSet<string> uiCommonGroup100 = new HashSet<string>();
//		HashSet<string> delGroup = new HashSet<string>();
//		Dictionary<string, int>.Enumerator ec = spriteCountMap.GetEnumerator();
//		while(ec.MoveNext())
//		{
//			if(ec.Current.Value > 1)
//			{
//				e = spriteGroup.GetEnumerator();
//				while(e.MoveNext())
//				{
//					e.Current.Value.Remove(ec.Current.Key);
//				}
//
//				if(!commonGroup.Contains(ec.Current.Key))
//				{
//					if(uiMainGroup.Contains(ec.Current.Key))
//					{
//						commonGroup.Add(ec.Current.Key);
//					}
//					else
//					{
//						uiCommonGroup100.Add(ec.Current.Key);
//					}
//				}
//			}
//		}
//
//
//		spriteGroup.Add("ui_common_100", uiCommonGroup100);
//
//
//
//
//
//
//
//		//find not found sprite
//		Dictionary<string, HashSet<string>> notFoundResSet = new Dictionary<string, HashSet<string>>();
//		string destPath = CreateSpriteSheet.bundleDir + "/" + "Sprite/UI/" + dir;
//		string[] files = Directory.GetFiles(destPath, "*.png", SearchOption.AllDirectories);
//		for(int i = 0; i < files.Length; i++)
//		{
//			bool found = false;
//			Dictionary<string, HashSet<string>>.Enumerator enumerator2 = spriteGroup.GetEnumerator();
//			while(enumerator2.MoveNext())
//			{
//				foreach(string path in enumerator2.Current.Value)
//				{
//					if(path == files[i])
//					{
//						found = true;
//						break;
//					}
//				}
//
//				if(found)
//				{
//					break;
//				}
//			}
//
//			if(!found)
//			{
//				notFoundResSet.Add(files[i], new HashSet<string>());
//			}
//		}
//
//
//
////		//separate group of rgb
////		{
////			Dictionary<string, HashSet<string>>.KeyCollection keys = spriteGroup.Keys;
////			Dictionary<string, HashSet<string>> newGroups = new Dictionary<string, HashSet<string>>();
////			foreach(string key in keys)
////			{
////				HashSet<string> set = spriteGroup[key];
////				HashSet<string> newSet = new HashSet<string>();
////				foreach(string path in set)
////				{
////					if(path.EndsWith(".jpg"))
////					{
////						newSet.Add(path);
////					}
////				}
////
////				foreach(string path in newSet)
////				{
////					set.Remove(path);
////				}
////
////				if(newSet.Count > 0)
////				{
////					newGroups.Add(key + "_rgb", newSet);
////				}
////			}
////
////			
////			Dictionary<string, HashSet<string>>.Enumerator ne = newGroups.GetEnumerator();
////			while(ne.MoveNext())
////			{
////				spriteGroup.Add(ne.Current.Key, ne.Current.Value);
////			}
////		}
//
//
//		//delete empty group
//		e = spriteGroup.GetEnumerator();
//		while(e.MoveNext())
//		{
//			if(e.Current.Value.Count <= 0)
//			{
//				delGroup.Add(e.Current.Key);
//			}
//		}
//		
//		foreach(string key in delGroup)
//		{
//			spriteGroup.Remove(key);
//		}
//
////		//merge small group
////		HashSet<string> commonGroup2 = new HashSet<string>();
////		HashSet<string> mergeGroup = new HashSet<string>();
////		e = spriteGroup.GetEnumerator();
////		while(e.MoveNext())
////		{
////			if(!e.Current.Key.EndsWith("_rgb") && e.Current.Value.Count <= 5)
////			{
////				mergeGroup.Add(e.Current.Key);
////				foreach(string spritePath in e.Current.Value)
////				{
////					commonGroup2.Add(spritePath);
////				}
////			}
////		}
//		
////		foreach(string group in mergeGroup)
////		{
////			spriteGroup.Remove(group);
////		}
//		
////		spriteGroup.Add("ui_common_100", commonGroup2);
//
//
//
//
//
//
//
//
//
//		//rename sprite path
//		Dictionary<string, SpriteSheetInfo> newSpriteGroup = new Dictionary<string, SpriteSheetInfo>();
//		e = spriteGroup.GetEnumerator();
//		while(e.MoveNext())
//		{
//			HashSet<string> newSet = new HashSet<string>();
//			foreach(string p in e.Current.Value)
//			{
//				string newName = p.Replace(CreateSpriteSheet.bundleDir + "/", "");
//				string fileName = Path.GetFileName(p);
//				string fileName2 = Path.GetFileNameWithoutExtension(p);
//				newName = newName.Replace(fileName, fileName2);
//				newSet.Add(newName);
//			}
//			SpriteSheetInfo info = new SpriteSheetInfo();
//			List<string> l = new List<string>(newSet);
//			info.dirs = l.ToArray();
//			info.is_from_interface = true;
//			info.multi_pack = true;
//			info.max_size = TexAttributeEx.MAX_SIZE;
////			info.scale = 0.5f;
//			newSpriteGroup.Add(e.Current.Key, info);
//		}
//
//
//
//
//
//
//		HashSet<string> modifySet = new HashSet<string>();
//		string modifyStr = CommonUtil.fileToString(EditorCommonTool.interfaceFile);
//		if(modifyStr != null)
//		{
//			modifySet = JsonConvert.DeserializeObject<HashSet<string>>(modifyStr);
//		}
//
//		//find modified groups
//		HashSet<string> modifySpriteSheets = new HashSet<string>();
//		SpriteSheetConfig config = EditorCommonTool.getRawSpriteSheetConfig(CreateSpriteSheet.bundleDir);
//		SpriteSheetConfig.Enumerator configE = config.GetEnumerator();
//		while(configE.MoveNext())
//		{
//			if(configE.Current.Value.is_from_interface)
//			{
//				for(int i  = 0; i < configE.Current.Value.dirs.Length; i++)
//				{
//					if(modifySet.Contains(configE.Current.Value.dirs[i])/* || configE.Current.Key.StartsWith("ui_")*/)
//					{
//						modifySpriteSheets.Add(configE.Current.Key);
//						break;
//					}
//				}
//			}
//		}
//
//		Dictionary<string, SpriteSheetInfo>.Enumerator es = newSpriteGroup.GetEnumerator();
//		while(es.MoveNext())
//		{
//			SpriteSheetInfo info = null;
//			if(!config.TryGetValue(es.Current.Key, out info))
//			{
//				config.Add(es.Current.Key, es.Current.Value);
//				modifySpriteSheets.Add(es.Current.Key);
//			}
//			else
//			{
//				if(info.dirs.Length != es.Current.Value.dirs.Length)
//				{
//					config[es.Current.Key] = es.Current.Value;
//					modifySpriteSheets.Add(es.Current.Key);
//				}
//				else
//				{
//					for(int i = 0; i < info.dirs.Length; i++)
//					{
//						bool found = false;
//						for(int j = 0; j < es.Current.Value.dirs.Length; j++)
//						{
//							if(es.Current.Value.dirs[j] == info.dirs[i])
//							{
//								found = true;
//								break;
//							}
//						}
//
//						if(!found)
//						{
//							config[es.Current.Key] = es.Current.Value;
//							modifySpriteSheets.Add(es.Current.Key);
//							break;
//						}
//					}
//				}
//			}
//		}
//
//		HashSet<string> delGroup2 = new HashSet<string>();
//		es = config.GetEnumerator();
//		while(es.MoveNext())
//		{
//			if(es.Current.Value.is_from_interface)
//			{
//				if(!newSpriteGroup.ContainsKey(es.Current.Key))
//				{
//					delGroup2.Add(es.Current.Key);
//					modifySpriteSheets.Add(es.Current.Key);
//				}
//			}
//		}
//		
//		foreach(string del in delGroup2)
//		{
//			config.Remove(del);
//		}
//		
//		if(modifyStr != null)
//		{
//			es = config.GetEnumerator();
//			while(es.MoveNext())
//			{
//				if(es.Current.Value.is_from_interface)
//				{
//					for(int i = 0; i < es.Current.Value.dirs.Length; i++)
//					{
//						string realdir = es.Current.Value.dirs[i];
//						if(modifySet.Contains(realdir))
//						{
//							modifySpriteSheets.Add(es.Current.Key);
//						}
//					}
//				}
//			}
//		}
//
//		Dictionary<string, int> spriteCountMap2 = new Dictionary<string, int>();
//		Dictionary<string, SpriteSheetInfo>.Enumerator configEnumerator = config.GetEnumerator();
//		while(configEnumerator.MoveNext())
//		{
//			string[] strs = configEnumerator.Current.Value.dirs;
//			for(int i = 0; i < strs.Length; i++)
//			{
//				if(spriteCountMap2.ContainsKey(strs[i]))
//				{
//					spriteCountMap2[strs[i]]++;
//				}
//				else
//				{
//					spriteCountMap2.Add(strs[i], 1);
//				}
//			}
//		}
//		Dictionary<string, int>.Enumerator countEnumerator = spriteCountMap2.GetEnumerator();
//		while(countEnumerator.MoveNext())
//		{
//			if(countEnumerator.Current.Value > 1)
//			{
//				Debug.LogError(countEnumerator.Current.Key + " repeated: " + countEnumerator.Current.Value);
//			}
//		}
//		
//		EditorCommonTool.setRawSpriteSheetConfig(config, CreateSpriteSheet.bundleDir);
//		MaterialTextureForETC1.modifySpritesheet(EditorCommonTool.filePath, modifySpriteSheets);
//
//
////		string str = JsonConvert.SerializeObject(newSpriteGroup, Formatting.Indented);
////		File.WriteAllText("test_sprite_sheet.txt", str);
////
////		string str5 = JsonConvert.SerializeObject(newSpriteGroup.Keys, Formatting.Indented);
////		File.WriteAllText("test_key.txt", str5);
//
////		string[] prefabs = Directory.GetFiles(CreateSpriteSheet.bundleDir, "*.prefab", SearchOption.AllDirectories);
////		Dictionary<string, HashSet<string>> prefabSpritePaths = new Dictionary<string, HashSet<string>>();
////		for(int i = 0; i < prefabs.Length; i++)
////		{
////			HashSet<string> spritePaths = MaterialTextureForETC1.getPrefabSpritePath(prefabs[i]);
////			if(spritePaths == null)
////			{
////				continue;
////			}
////
////			bool found = false;
////			foreach(string s in spritePaths)
////			{
////				if(s.Contains("UIInterface"))
////				{
////					found = true;
////					break;
////				}
////			}
////
////			Resources.UnloadUnusedAssets();
////			System.GC.Collect();
////
////			if(!found)
////			{
////				continue;
////			}
////
////			prefabSpritePaths.Add(prefabs[i], spritePaths);
////		}
////		foreach(string res in notFoundResSet.Keys)
////		{
////			Dictionary<string, HashSet<string>>.Enumerator enu3 = prefabSpritePaths.GetEnumerator();
////			while(enu3.MoveNext())
////			{
////				foreach(string p in enu3.Current.Value)
////				{
////					if(res == p)
////					{
////						notFoundResSet[res].Add(p);
////						break;
////					}
////				}
////			}
////		}
//		string str3 = JsonConvert.SerializeObject(notFoundResSet, Formatting.Indented);
//		CommonUtil.stringToFile("not_found_sprite.txt", str3);
//	}
//
////	[MenuItem("Custom/Sprite/ArrangeUI")]
////	private static void ArrangeUI()
////	{
////		string destPath = CreateSpriteSheet.bundleDir + "/" + "Sprite/UI/UIInterface";
////		Directory.CreateDirectory(destPath);
////
////		string[] exts = new string[]{"*.png", "*.jpg"};
////		string[] paths = new string[]{"Sprite/UI/ArtSet", "Sprite/UI/UIBase", "Sprite/UI/UICity", "Sprite/UI/UIInterface", "Sprite/UI/UIMain"};
////		for(int i = 0; i < paths.Length; i++)
////		{
////			for(int j = 0; j < exts.Length; j++)
////			{
////				string[] filePaths = Directory.GetFiles(CreateSpriteSheet.bundleDir + "/" + paths[i], exts[j], SearchOption.AllDirectories);
////				for(int k = 0; k < filePaths.Length; k++)
////				{
////					string fileName2 = Path.GetFileNameWithoutExtension(filePaths[k]);
////					while(true)
////					{
////						string[] saveNameFiles = Directory.GetFiles(destPath, fileName2 + ".*", SearchOption.TopDirectoryOnly);
////						if(saveNameFiles == null || saveNameFiles.Length <= 0)
////						{
////							break;
////						}
////						fileName2 = fileName2 + "+";
////					}
////					string ext = Path.GetExtension(filePaths[k]);
////					string ret = AssetDatabase.MoveAsset(filePaths[k], destPath + "/" + fileName2 + ext);
////					if(!string.IsNullOrEmpty(ret))
////					{
////						Debug.Log(ret);
////					}
////				}
////			}
////		}
////	}
//
//
////	[MenuItem("Custom/Prefab/PrintWindowInfo")]
////	public static void PrintWindowInfo()
////	{
////		List<WindowPrefabInfo> allWindowInfo = new List<WindowPrefabInfo>();
////		Type[] allType = Assembly.GetAssembly(typeof(Window)).GetTypes();
////		for (int i = 0; i < allType.Length;i++ )
////		{
////			Type t= allType[i];
////			if (!t.IsSubclassOf(typeof(Window)) || t.Equals(typeof(Window)) || t.Equals(typeof(PopupWindow)) || t.Equals(typeof(Dialog)) || t.Equals(typeof(NativeWindow)) || t.IsSubclassOf(typeof(NativeWindow)))
////			{
////				continue;
////			}
////			WindowPrefabInfo info = GatherWindowPrefabRef(t);
////			allWindowInfo.Add(info);
////		}
////
////		string str = JsonConvert.SerializeObject(allWindowInfo, Formatting.Indented);
////		CommonUtil.stringToFile("window_info.txt", str);
////	}
}